package com.yfp.machine.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.machine.domain.ApkVersion;
import com.yfp.machine.domain.vo.ApkVersionPushVo;
import com.yfp.machine.service.ApkVersionService;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * (ApkVersion)表控制层
 *
 *
 * @since 2023-12-14 15:00:35
 */
@RestController
@RequestMapping("apkVersion")
@Tag(name = "版本推送")
public class ApkVersionController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ApkVersionService apkVersionService;

    @Operation(summary =  "设备对应app列表")
    @GetMapping("/appList")
    public R appList(@ApiParam("设备类型")Integer machineType,String machineId) {
        return R.ok(this.apkVersionService.findCorrespondingAppVersionByMachineType(machineType,machineId));
    }


    @Operation(summary = "推送App版本")
    @PostMapping("push")
    @Log(title = "推送App版本", businessType = BusinessType.UPDATE)
    public R push(@RequestBody @Valid ApkVersionPushVo apkVersionPushVos) {
        return this.apkVersionService.pushApkVersion(apkVersionPushVos) ?
                R.ok(null, "推送成功") : R.fail("推送失败");
    }


    @Operation(summary = "分页获取APK")
    @GetMapping
    public TableDataInfo queryByPage(ApkVersion apkVersion,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        startPage();
        return getDataTable(this.apkVersionService.queryApkVersion(apkVersion));
    }

    @Operation(summary = "获取APK详情")
    @GetMapping("{versionNo}")
    public R queryById(@PathVariable("versionNo") String versionNo) {
        return R.ok(this.apkVersionService.queryById(versionNo));
    }

    @Operation(summary = "新增APK")
    @PostMapping
    @Log(title = "新增APK", businessType = BusinessType.INSERT)
    public R insert(@RequestBody @Valid ApkVersion apkVersion) {
        return R.ok(this.apkVersionService.insert(apkVersion));
    }

    @Operation(summary = "修改APK")
    @PutMapping
    @Log(title = "修改APK", businessType = BusinessType.UPDATE)
    public R update(@RequestBody @Valid ApkVersion apkVersion) {
        return R.ok(this.apkVersionService.update(apkVersion));
    }

    @Operation(summary = "删除APK")
    @PutMapping("/{versionNo}")
    @Log(title = "删除APK", businessType = BusinessType.DELETE)
    public R deleteById(@PathVariable String versionNo) {
        this.apkVersionService.updateIsDelete(versionNo);
        return R.ok();
    }

}

